import { Component, OnInit } from '@angular/core';
import {APARTMENT} from "../../service/apartment.service";
import {ActivatedRoute} from "@angular/router";
import {FormBuilder} from "@angular/forms";
import {WfToast} from "../../utils/toast.utils";
import {GOODS} from "../../service/goods-api";
import {CheckStandModel} from "../../model/checkstand.model";
import {LocalStorage} from "../../app.local-storage";
import {ROUTE} from "../../service/route.service";
import {selectDate} from "../../lib/datepicker/datepicker.component";


@Component({
  selector: 'app-score-mall-detail-view',
  templateUrl: './score-mall-detail-view.component.html',
  styleUrls: ['./score-mall-detail-view.component.scss']
})
export class ScoreMallDetailViewComponent implements OnInit {
  layoutList = [];
  data = {
    layoutId:undefined,
    startTime:new selectDate('')
  };

  goodsId:number;
  totalMoney:number=0;
  currentValue:number = 0;
  defaultImg:string='assets/images/brand-story.jpg';
  goods:any={};
  banners:any=[
    {id:1,url:'assets/images/brand-story.jpg'},
    {id:2,url:'assets/images/brand-story.jpg'},
    {id:3,url:'assets/images/brand-story.jpg'},
    {id:4,url:'assets/images/brand-story.jpg'}
  ];
  leaseTime:any=[
    {name: '1月', value: 1,time:1},
    {name: '3月', value: 2,time:3},
    {name: '6月', value: 3,time:6},
    {name: '1年', value: 4,time:12},
    {name: '2年', value: 5,time:24}
  ];

  constructor(private apartmentApi:APARTMENT,
              private routeInfo:ActivatedRoute,
              private toast:WfToast,
              private GOODS:GOODS,
              private route:ROUTE,
              private ls:LocalStorage,
              private formBuilder:FormBuilder) {

  }
  loadMyHouse() {
    this.apartmentApi.getMyRentingHouse({}, (res:any)=> {
      this.layoutList = res;
      if(this.layoutList.length > 0){
        this.data.layoutId = this.layoutList[0].id
      }
    })
  }
  getDate(event: selectDate) {
    this.data.startTime = event;
  }

  ngOnInit() {
    this.routeInfo.params.subscribe((param:any)=>{
      this.goodsId = param.id;
      this.apartmentApi.getGoodsDetail(this.goodsId,(res:any)=>{
        console.log(res);
        this.goods=res;
        this.getTotalMoney(1);
        res.banners?this.banners = res.banners:this.banners;
      })
    });
    this.loadMyHouse();
  }
  setImgUrl(item:any){
    // console.log(item);
    this.defaultImg = item;
  }
  getTotalMoney(item:any){
    this.totalMoney = item*this.goods.salesPrice;
  }
  getTimeByValue(value):number{
    for(let v of this.leaseTime){
      if (v.value == value){
        return v.time;
      }
    }

  }

  doRenting(item:any){
    let reqData = {
      totalMoney:0,
      houseId: this.data.layoutId,
      goodsId: this.goodsId,
      source: 2,
      score:this.goods.score
    }
    this.GOODS.purchaseScore(reqData,v=>{
      this.toast.msg("兑换成功，请去兑换记录中查看")
    })
  }
}
